package com.yxhl.zoume.core.officialcar.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.custom.QuicklyClearEditText;
import com.yxhl.zoume.common.ui.fragment.BaseFragment;
import com.yxhl.zoume.core.officialcar.activity.CallCarForOthersActivity;
import com.yxhl.zoume.core.car.model.CarSelectPassengerFromContactEvent;
import com.yxhl.zoume.data.http.model.account.ZMAccount;
import com.yxhl.zoume.utils.AccountUtils;
import com.yxhl.zoume.utils.LOG;
import com.yxhl.zoume.utils.SoftInputUtils;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import rx.functions.Action1;

/**
 * A simple {@link Fragment} subclass.
 */
public class CallCarForOthersFragment extends BaseFragment {

    private static final String TAG = "CallCarForOthersFragment";

    @BindView(R.id.qce_fragment_call_car_for_others_mobile)
    QuicklyClearEditText mOthersMobileQce;

    @BindView(R.id.tv_fragment_call_car_for_others_contact)
    TextView mOthersContractTv;

    @BindView(R.id.cb_fragment_call_car_for_others_notice)
    CheckBox mOthersNoticeCb;


    public CallCarForOthersFragment() {
        // Required empty public constructor
    }

    public static CallCarForOthersFragment newInstance() {
        CallCarForOthersFragment fragment = new CallCarForOthersFragment();
        return fragment;
    }

    private void onRxBusEvent() {
        getSubscription().add(toSubscription(CarSelectPassengerFromContactEvent.class,
                new Action1<CarSelectPassengerFromContactEvent>() {
                    @Override
                    public void call(CarSelectPassengerFromContactEvent carSelectPassengerFromContactEvent) {
                        //显示页面
                        ((CallCarForOthersActivity) mActivity).displayOneFragment(CallCarForOthersActivity.FRAGMENT_CALL_CAR_FOR_OTHERS);

                        String passengerMobile = carSelectPassengerFromContactEvent.getPassengerMobile().trim();
                        if (!TextUtils.isEmpty(passengerMobile)) {
                            if (mOthersMobileQce != null) {
                                mOthersMobileQce.setText(passengerMobile);
                                mOthersMobileQce.requestFocus();
                                //设置光标位置
                                mOthersMobileQce.setSelection(passengerMobile.length());
                            }
                        }
                    }
                },
                new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LOG.e(TAG, "throwable=" + throwable.toString());
                        onRxBusEvent();
                    }
                }
        ));
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onRxBusEvent();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        ZMAccount account = AccountUtils.getInstance(mActivity).getAccount();
        if (null == account) {
            mOthersMobileQce.setText("");
        } else {
            mOthersMobileQce.setText(account.getAccountName());
        }

        return view;
    }


    @OnTextChanged(R.id.qce_fragment_call_car_for_others_mobile)
    void onMobileTextChange(CharSequence s, int start, int before, int count) {
        if (s != null && s.length() >= 11) {
            // TODO: 16/8/15 检验乘车人列表选中的手机号,RxBusEvent回调,是否会触发此方法
            LOG.e(TAG, "onMobileTextChange");
            ((CallCarForOthersActivity) mActivity).setSelectPassengerMobile(s.toString());
        }
    }


    @OnClick(R.id.tv_fragment_call_car_for_others_contact)
    void onOthersContactClick() {
        ((CallCarForOthersActivity) mActivity).displayOneFragment(CallCarForOthersActivity.FRAGMENT_CAR_SELECT_PASSENGER);
    }


    /**
     * 是否短信通知乘车人
     *
     * @param buttonView
     * @param isChecked
     */
    @OnCheckedChanged(R.id.cb_fragment_call_car_for_others_notice)
    void onOthersNoticeChecked(CompoundButton buttonView, boolean isChecked) {
        ((CallCarForOthersActivity) mActivity).setNeedSendMsg(isChecked);
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            SoftInputUtils.closeInputKeyboard(mActivity);
            setupToolBar();
        }
    }

    @Override
    protected void initializeInjector() {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_call_car_for_others;
    }

    @Override
    protected void setupToolBar() {
        if (mActivity instanceof CallCarForOthersActivity) {
            CallCarForOthersActivity containerActivity = (CallCarForOthersActivity) mActivity;
            containerActivity.setToolbar(R.string.fragment_call_car_for_others_header_title, "完成");
        }
    }
}
